﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MnNius.SmallProgram.API;
using ShenNius.ModuleCore;
using ShenNius.ModuleCore.Context;
using ShenNius.ModuleCore.Extensions;

namespace MnNius.API.Hosting
{
    [DependsOn(
        typeof(MnNiusSmallProgramApiModule))]
    public class MnNiusApiHostingModule:AppModule
    {
        public override void OnConfigureServices(ServiceConfigurationContext context)
        {

            context.Services.AddControllers();
            //禁用默认验证输出行为
            context.Services.Configure<ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });
        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = ServiceProviderServiceExtensions.GetRequiredService<IWebHostEnvironment>(context.ServiceProvider);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
           
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                //全局路由配置
                endpoints.MapControllerRoute(
                     name: "default",
                       pattern: "{controller=Home}/{action=Index}/{id?}"
                    );
            });
        }
    }
}
